package org.xfc.util;

import java.io.IOException;

import org.xfc.testing.XMockApp;
import org.xfc.testing.XUITestCase;

public class XTaskTest
	extends XUITestCase
{
	private XTask 	task;
	
	private boolean throwException;
	
	private boolean	taskStarted;
	private boolean	taskRan;
	private boolean	taskErrored;
	
	protected void setUp()
	{
		XMockApp.init();
		
		task = new XTask("Test Task") {
			protected Object performWork() throws IOException {
				taskStarted = true;
				
				if (throwException)
					throw new IOException("cannot open file");
				
				return null;
			}
			protected void taskPostFlightException(Throwable t) {
				taskErrored = true;
			}
			protected void taskPostFlightSuccess(Object result) {
				taskRan = true;
			}
		};
		
		taskStarted = false;
		taskRan = false;
		taskErrored = false;
		
		throwException = false;
	}
	
	protected void tearDown()
	{
		XMockApp.dispose();
	}
	
	public void testCompleted()
	{
		task.start();
		
		waitForIdle();
		waitFor(hasProperty("processingTasks", eq(false)), XTaskManager.getTaskManager());
		
		assertThat(taskStarted, eq(true));
		assertThat(taskRan, eq(true));
		assertThat(taskErrored, eq(false));
	}
	
	public void testErrored()
	{
		throwException = true;
		
		task.start();
		
		waitForIdle();
		waitFor(hasProperty("processingTasks", eq(false)), XTaskManager.getTaskManager());
		
		assertThat(taskStarted, eq(true));
		assertThat(taskRan, eq(false));
		assertThat(taskErrored, eq(true));
	}
	
}
